package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckitemController {

    @Autowired
    private CheckitemService checkitemService;

    /**
     *新增检查项
     * @return
     */
    @PostMapping("/add")
    public Result save(@RequestBody CheckItem checkItem){
        log.info("新增检查项:{}", checkItem);
        checkitemService.save(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     *分页查询
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}", queryPageBean);
        return checkitemService.pageQuery(queryPageBean);
    }

    /**
     * 查询所有的检查项
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckItem> checkItemList = checkitemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItemList);
    }

    /**
     * 根据检查组id查询需要勾选的检查项id
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkgroupId){
        List<Integer> checkItems =  checkitemService.findCheckItemIdsByCheckGroupId(checkgroupId);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);
    }

    /**
     * 删除检查项
     */
    @GetMapping("/delete")
    public Result deleteById(Integer id){
        log.info("删除检查项ID为：{}", id);
        if(checkitemService.deleteById(id)){
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    /**
     * 查询检查项
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("查询检查项ID:{}", id);
        CheckItem checkItem = checkitemService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 编辑检查项
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestBody CheckItem checkItem){
        log.info("编辑检查项:{}", checkItem);
        checkitemService.update(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}
